package org.example.comment;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Component
@Slf4j
public class ZiDongTianChong implements MetaObjectHandler {
    /**
     * 新增数据时进行填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("开始进行数据填充");
        this.setFieldValByName("createTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName("updateTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName("createUser", BaseContext.getCurrentId(),metaObject);
        this.setFieldValByName("updateUser", BaseContext.getCurrentId(),metaObject);
    }

    /**
     * 修改数据时进行填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        log.info("开始进行数据填充");
        log.info("当前线程id为：{}",Thread.currentThread().getId());
        this.setFieldValByName("updateTime", LocalDateTime.now(),metaObject);
        this.setFieldValByName("updateUser", BaseContext.getCurrentId(),metaObject);

    }
}

